﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using Sunny.UI;

namespace IndustryFieldCommunication
{
    public partial class FEPPallet : UIPage
    {
        public FEPPallet(Socket client)
        {
            InitializeComponent();

            InProcess = (taskID) =>
            {
                var raw = TaskList.Rows[IDRowMap[taskID]];
                var task = (TaskObj)raw.Cells[2].Value;
                task.State = "进行";
                raw.Cells[1].Value = task.State;
            };
            Canceled = (taskID) =>
            {
                var raw = TaskList.Rows[IDRowMap[taskID]];
                var task = (TaskObj)raw.Cells[2].Value;
                task.State = "取消";
                raw.Cells[1].Value = task.State;
            };
            Done = (taskID) =>
            {
                var raw = TaskList.Rows[IDRowMap[taskID]];
                var task = (TaskObj)raw.Cells[2].Value;
                task.State = "完成";
                raw.Cells[1].Value = task.State;
                raw.Cells[0].Value = "";
            };
            Monitored = (Data) =>
            {
                var rows = DataGridViewMonitor.Rows;
                for (int i = 0; i < Data.Length; ++i)
                {
                    rows[i+1].Cells[1].Value = Data[i];
                }
            };
            CheckHeartBeat = (Responed) =>
            {
                var firstRow = DataGridViewMonitor.Rows[0];
                if (Responed)
                    firstRow.Cells[1].Value = "是";
                else
                    firstRow.Cells[1].Value = "否";
            };
            MsgProcess = new(client, InProcess, Canceled, Monitored, Done, CheckHeartBeat, ref OnDispose);
            var rows = DataGridViewMonitor.Rows;
            for (int i = 0; i < DataDescript.Length; ++i)
            {
                DataGridViewMonitor.AddRow();
                rows[i].Cells[0].Value = DataDescript[i];
            }
        }

        private void TaskList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                if ((string)TaskList.CurrentCell.Value == "取消")
                {
                    var raw = TaskList.CurrentRow;
                    var task = (TaskObj)raw.Cells[2].Value;
                    MsgProcess.CancelTask(task.ID);
                    TaskList.CurrentCell.Value = "";
                }
            }
        }

        private void BTConfirm_Click(object sender, EventArgs ea)
        {
            try
            {
                var str = MaskedTextBox.Text;
                string[] delimiters = { "，长", "，宽", "，高", "；" };
                var Params = str.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                var ID = MsgProcess.CreateTask(Params[1].ToInt(), Params[2].ToInt(), Params[3].ToInt());
                if (ID.StartsWith("无效参数"))
                    throw new Exception(ID);
                var task = new TaskObj("码垛", ID, str.Remove(0, 3), "等待");
                var raw = TaskList.AddRow("取消", task.State, task);
                IDRowMap.Add(ID, raw);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), "在BTConfirm_Click", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }

        private void CheckBoxMonitor_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBoxMonitor.Checked)
                MsgProcess.StartMonitor(TextBoxSpan.Text.ToDouble());
            else
                MsgProcess.StopMonitor();
        }

        MsgProcess MsgProcess;
        Dictionary<string, int> IDRowMap = new();
        Action<string> InProcess;
        Action<string> Canceled;
        Action<string[]> Monitored;
        Action<string> Done;
        Action<bool> CheckHeartBeat;
        public EventHandler OnDispose;
        string[] DataDescript = { "响应", "任务号", "任务进度", "夹爪使用次数", "出货量", "准备工作" };

        private class TaskObj
        {
            public string Name;
            public string ID;
            public string Params;
            public string State;

            public override string ToString()
            {
                return Name + "，" + "任务号" + ID + "，" + Params;
            }
            public TaskObj(string _Name, string _ID, string _Params, string _State)
            {
                Name = _Name;
                ID = _ID;
                Params = _Params;
                State = _State;
            }
        }
    }
}
